<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button preIcon="ant-design:plus-outlined" type="primary" @click="handleAdd" style="margin-right: 5px">新增</a-button>
        <a-button type="primary" preIcon="ant-design:edit-outlined" @click="handleOpenGroup">功能组</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
    <TaskDrawer @register="registerTaskDrawer" @success="reload" showFooter/>
    <TaskDetail @register="registerTaskDetail"/>
    <MessageFieldDrawer @register="registerMessageFieldDrawer"/>
    <TaskTemplateDrawer @register="registerTaskTemplateDrawer"/>
    <ReceiverTableDrawer @register="registerReceiverTableDrawer"/>
    <MyDataGroupListDrawer @register="registerGroupDrawer" @success="reload" :bizType="DataGroupBizType.MSG_PUSH_TASK"/>
  </div>
</template>
<script lang="ts" name="MsgTask" setup>
import {ActionItem, BasicTable, TableAction} from '/@/components/Table';
import TaskDetail from './TaskDetail.vue';
import {useDrawer} from '/@/components/Drawer';
import {
  batchDeleteGroup,
  batchDeleteTask,
  deleteGroup,
  deleteTask,
  listTask,
  saveOrUpdateGroup
} from './task.api';
import {columns, searchFormSchema} from './task.data';
import {ReceiverType} from '../receiver/receiver.data';
import TaskDrawer from './TaskDrawer.vue';
import {useMessage} from "/@/hooks/web/useMessage";
import {useListPage} from '/@/hooks/system/useListPage'
import MessageFieldDrawer from './components/MessageFieldDrawer.vue';
import ReceiverTableDrawer from './components/ReceiverTableDrawer.vue';
import TaskTemplateDrawer from '../template/components/TaskTemplateDrawer.vue';
import {DataGroupBizType, MyDataGroupListDrawer} from "/@/components/BizDataGroup/index"
import {Icon} from '/@/components/Icon'

const [registerTaskDrawer, { openDrawer: openTaskDrawer }] = useDrawer()
const [registerTaskDetail, { openDrawer: openTaskDetail }] = useDrawer()
const [registerGroupDrawer, { openDrawer: openGroupDrawer }] = useDrawer();
const [registerMessageFieldDrawer, { openDrawer: openMessageFieldDrawer }] = useDrawer()
const [registerTaskTemplateDrawer, { openDrawer: openTaskTemplateDrawer }] = useDrawer()
const [registerReceiverTableDrawer, { openDrawer: openReceiverTableDrawer }] = useDrawer()
const { createMessage } = useMessage();

// 列表页面公共参数、方法
const { prefixCls, tableContext } = useListPage({
  designScope: 'msg-task',
  tableProps: {
    title: '消息任务列表',
    api: listTask,
    pagination: false,
    bordered: false,
    actionColumn: {
      width: 150,
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      slots: { customRender: 'action' },
    },
    columns: columns,
    formConfig: {
      schemas: searchFormSchema,
      fieldMapToTime: [['fieldTime', ['beginDate', 'endDate'], 'YYYY-MM-DD HH:mm:ss']]
    }
  }
})
const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

/**
 * 操作列定义
 * @param record
 */
function getTableAction(record) {
  return [
    {
      label: '删除',
      color: 'error',
      popConfirm: {
        title: `是否确认删除 ${record.name}`,
        confirm: handleDelete.bind(null, record),
      },
    }, 
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
    }
  ]
}

/**
 * 任务详情
 */
const handleTaskDetail = (record) => {
  openTaskDetail(true, { record });
}

/**
 * 下拉操作栏
 */
 function getDropDownAction(record): ActionItem[] {
  return [
    {
      label: '详情',
      onClick: handleTaskDetail.bind(null, record),
    },
    {
      label: '模板管理',
      onClick: handleOpenTemplate.bind(null, record),
    },
    {
      label: '字段管理',
      onClick: handleOpenMessageField.bind(null, record),
    },
    {
      label: '接收人',
      ifShow: (e) => record.receiverType === ReceiverType.ADMIN, // 2为管理员
      onClick: handleOpenReceiverTableDrawer.bind(null, record),
    }
  ]
}


/**
 * 新增事件
 */
function handleAdd() {
  openTaskDrawer(true, {
    isUpdate: false,
  });
}

/**
 * 编辑事件
 */
function handleEdit(record) {
  openTaskDrawer(true, {
    record,
    isUpdate: true,
  });
}

/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteTask({ id: record.id }, reload);
}

/**
 * 批量删除事件
 */
async function batchHandleDelete() {
  await batchDeleteTask({ ids: selectedRowKeys.value }, reload);
}

/**
 * 打开模板
 */
function handleOpenTemplate(record) {
  openTaskTemplateDrawer(true, { record  });
}

/**
 * 配置消息字段
 * @param record 
 */
function handleOpenMessageField(record) {
  openMessageFieldDrawer(true, { record });
}

/**
 * 打开消息接收人列表
 */
function handleOpenReceiverTableDrawer(record) {
  openReceiverTableDrawer(true, { record });
}

function handleOpenGroup() {
  openGroupDrawer(true, {
    title: '消息推送功能组',
    width: 800,
    deleteApi: deleteGroup,
    batchDeleteApi: batchDeleteGroup,
    saveOrUpdateApi: saveOrUpdateGroup
  })
}
</script>
